
#import <IMSLog/IMSLog.h>
#import <ALBBOpenAccountCloud/ALBBOpenAccountSDK.h>
#import <IMSIotSmart/IMSIotSmart.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-umbrella"
#import <IMSIotSmart/IMSIotSmart+options.h>
#pragma clang diagnostic pop

#import "AliIOT_RAC.h"
#import "ReactiveObjC.h"

#import "LaunchManager.h"

#import "AlinkManager.h"

@interface AliIOT_RAC () {
}
@end

@implementation AliIOT_RAC

+ (AliIOT_RAC *)sharedInstance {
    static AliIOT_RAC *Instance;
    @synchronized(Instance) {
        if (!Instance) {
            Instance = [[AliIOT_RAC alloc] init];
        }
    }
    return Instance;
}

- (void)dealloc {
}

- (id)init {
    self = [super init];
    if (self) {
        id delegate = [UIApplication sharedApplication].delegate;

        [[delegate rac_signalForSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:) fromProtocol:@protocol(UIApplicationDelegate)] subscribeNext:^(RACTuple *tupe) {
          UIApplication *application = tupe.first;
          NSData *deviceToken = tupe.second;
          [[IMSIotSmart sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
          //保存deviceToken
          [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"IOTDeviceToken"];
          [[NSUserDefaults standardUserDefaults] synchronize];
        }];

        // RAC 回调
        [[delegate rac_signalForSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:) fromProtocol:@protocol(UIApplicationDelegate)] subscribeNext:^(RACTuple *tupe) {
          UIApplication *application = tupe.first;
          NSError *error = tupe.second;
          NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
          [[IMSIotSmart sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
        }];

        // RAC 回调
        // Required,For systems with less than or equal to iOS6
        [[delegate rac_signalForSelector:@selector(application:didReceiveRemoteNotification:) fromProtocol:@protocol(UIApplicationDelegate)] subscribeNext:^(RACTuple *tupe) {
          UIApplication *application = tupe.first;
          NSDictionary *userInfo = tupe.second;
          [[IMSIotSmart sharedInstance] application:application didReceiveRemoteNotification:userInfo];
        }];
    }
    return self;
}

- (void)iotSetUp:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /**
    NSString *authCode;
    NSString *serverEnv;
    NSString *boneEnv;
#if __has_include(<IMSDebug/IMSDebug.h>)
    NSDictionary *dict = [[IMSDebugEnv sharedInstance] reSetDebugDict];
    authCode = dict[@"authCode"];
    serverEnv = dict[@"serverEnv"];
    boneEnv = dict[@"boneEnv"];
#else
    authCode = @"product";
    serverEnv = @"release";
    boneEnv = @"release";
#endif

    IMSIotSmartConfig *config = [IMSIotSmartConfig new];
    config.regionType = REGION_ALL;
    if ([authCode isEqualToString:@"product"]) {
        config.appType = APP_TYPE_PRODUCTION;
    } else {
        config.appType = APP_TYPE_DEVELOP;
    }
    [[IMSIotSmart sharedInstance] setConfig:config];

    IMSIotSmartEnvironment *env = [[IMSIotSmartEnvironment alloc] init];
    env.serverEnv = serverEnv;
    env.boneEnv = boneEnv;
    [[IMSIotSmart sharedInstance] setEnvironment:env];
    [[IMSIotSmart sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
     */

    //设置日志选项
    [IMSLog setAllTagsLevel:IMSLogLevelWarning];
    [IMSLog showInConsole:YES];
    [IMSLog registerTag:IMS_DEMO_TAG];
    [[LaunchManager defaultManager] startLaunch];

    [AlinkManager attempDealloc];
    [AlinkManager shareManager];
}

@end
